﻿/*
20110419
no override width/height by textWidth, textHeight, will cause background area fail.
20110414
add this.text , this.htmlText
next time use super.xxxx to override basic function. fool....
20101024
DEL: Event.ENTER_FRAME, 使用 override getter 的方式利用 htmltext 與 text 共通, 強行重寫 this.text 功能.
並於其中加插 update();
20101023
Fix 輸入文字未能自動 update(); // Event.ENTER_FRAME , 佔資源
20101020
把 width & height, 準確地回傳目前 Text 的數值需使用 update();






*/
package com.cf.core.view{
	import flash.text.*;
	import flash.events.Event;
	public class MyText extends TextField{
		private var _format:TextFormat;
		public function get style():TextFormat{return _format}
		public function set style(_o:TextFormat):void{_format=_o; resetformat();}	// Just in case. don't use this to change.
		
		public static const LEFT:String = TextFormatAlign.LEFT;
		public static const CENTER:String = TextFormatAlign.CENTER;
		public static const RIGHT:String = TextFormatAlign.RIGHT;
		
		public function set family(_s:String):void{style.font=_s; resetformat();}
		public function get family():String{return style.font}
		
		public function set bold(_b:Boolean):void{style.bold=_b; resetformat(); update();}
		public function get bold():Boolean{return style.bold}
		public function set italic(_b:Boolean):void{style.italic=_b; resetformat(); update();}
		public function get italic():Boolean{return style.italic}
		public function set underline(_b:Boolean):void{style.underline=_b; resetformat(); update();}
		public function get underline():Boolean{return style.underline}
		public function set size(_n:Object):void{style.size=_n; resetformat(); update();}
		public function get size():Object{return style.size}
		
		public function set wrap(_t:Boolean):void{this.multiline=_t; this.wordWrap=_t;}
		public function get wrap():Boolean{return (this.multiline && this.wordWrap);}
		
		public function set align(_o:String):void{style.align=_o; resetformat();}
		public function get align():String{return style.align}
		
		public function set color(_n:Object):void{style.color=_n; resetformat();}
		public function get color():Object{return style.color}
		
		public function set editable(_t:Boolean):void{this.type=(_t)? TextFieldType.INPUT : TextFieldType.DYNAMIC;}
		public function get editable():Boolean{return (this.type==TextFieldType.INPUT)?true:false}
		
//		override public function get width():Number{update(); return this.textWidth}
//		override public function get height():Number{update(); return this.textHeight}
		override public function set text(_s:String):void{super.text=_s;update();}
		override public function set htmlText(_s:String):void{super.htmlText=_s;update();}
		
		public function MyText():void{
			_format = new TextFormat();
			// My default setting (^^)
			this.selectable=false;
			this.borderColor=0x000000;
			this.backgroundColor=0xFFFFFF;
			this.background=false;
			this.border=false;
			this.editable=false;
			this.useRichTextClipboard=true;
			this.autoSize=TextFieldAutoSize.LEFT;
			this.wrap=false;
		}//fn
		
		public function embedfont(_fn:String):void{
			// we need to embedfont on scene first.
			var eFonts:Array = Font.enumerateFonts();
			var flag:Boolean = false;
			for(var j in eFonts ){
				if(eFonts[j].fontName==_fn) flag=true;
			}//for
			if(flag){
				this.family = _fn;
				this.embedFonts=true;
				this.antiAliasType=AntiAliasType.ADVANCED;
			}else{
				trace("Font assign Fail["+_fn+"] : you need to embed font on the scene first");
			}//if
		}//fn
		public function update():void{	// update Font size
			this.width = this.textWidth+(this.textWidth*0.3);  // margin
			this.height = this.textHeight+(this.textHeight*0.3); // margin
		}//fn
		private function resetformat():void{
			var tmp:String = this.text;
			this.defaultTextFormat=style;
			this.text = tmp;
		}//fn
		
	}//class
}//package